package com.sky.demo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.sky.demo.R;

import java.util.List;

 /**
 * @author 彬 QQ 1136096189
 * @Description: 热cyclerview
 * @date 2015/8/17 15:30
 */
public class RecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> {

    protected Context context;
    protected List<String> mDatas;
    protected static final int LASTITEM = -1;
    protected OnItemClickListener onItemClickListener;

    public RecyclerAdapter(Context context, List<String> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    public List<String> getmDatas() {
        return mDatas;
    }

    public void setmDatas(List<String> mDatas) {
        this.mDatas = mDatas;
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.tv, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.tv.setText(mDatas.get(position));
        holder.tv.setGravity(Gravity.CENTER);
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Toast.makeText(context,holder.tv.getText().toString(),Toast.LENGTH_SHORT).show();
                    int layoutPosition = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, layoutPosition);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    onItemClickListener.onItemLongClick(holder.itemView, layoutPosition);
                    return false;
                }
            });
        }
    }
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 添加item
     *
     * @param text
     * @param position -1时最后一个添加
     */
    public void addItem(String text, int position) {
        position = position == LASTITEM ? getItemCount() : position;
        mDatas.add(position, text);
        notifyItemInserted(position);
    }

    /**
     * 删除item
     * @param position
     */
    public void deleteItem(int position) {
        if (position == LASTITEM && getItemCount() > 0)
            position = getItemCount() - 1;
        if (position>LASTITEM&&position < getItemCount()) {
            mDatas.remove(position);
            notifyItemRemoved(position);
        }
    }
}

class MyViewHolder extends RecyclerView.ViewHolder {

    TextView tv;

    public MyViewHolder(View itemView) {
        super(itemView);
        tv = (TextView) itemView.findViewById(R.id.tv);
    }
}
